package smartcampus.scjava.controller.forth;


import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import smartcampus.scjava.POJO.DO.Book;
import smartcampus.scjava.POJO.DO.Equipment;
import smartcampus.scjava.POJO.DO.EquipmentInventoryRecord;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.IEquipmentInventoryRecordService;
import smartcampus.scjava.service.IEquipmentService;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 器材表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2023-12-05
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {

    @Resource
    private IEquipmentService equipmentService;

    @Resource
    private IEquipmentInventoryRecordService inventoryRecordService;


    @RequestMapping("/listPage")
    public ResultVO<?> listPage(Integer  pageNum, Integer pageSize, String word) {
        if (pageNum == null || pageSize == null) {
            return ResultVO.error("分页参数错误");
        }

        Page<Equipment> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Equipment> query = new QueryWrapper<>();
        if (StringUtils.hasText(word)) {
            query.lambda().like(Equipment::getEquipmentName,word).
                    or().like(Equipment::getEquipmentNo,word);
        }


        equipmentService.page(page,query);

        return ResultVO.success(page);
    }

    @RequestMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<?> add(@RequestBody Equipment equipment) {

        Equipment byEquipmentNo = equipmentService.getByEquipmentNo(equipment.getEquipmentNo());
        if (byEquipmentNo != null) {
            return ResultVO.error("器材编号已存在");
        }

        equipment.setCreateTime(LocalDateTime.now());
        equipment.setUpdateTime(LocalDateTime.now());
        equipmentService.save(equipment);
        EquipmentInventoryRecord one = inventoryRecordService.getOne(new LambdaQueryWrapper<EquipmentInventoryRecord>().
                eq(EquipmentInventoryRecord::getEquipmentName, equipment.getEquipmentName()));
        if (one != null) {
            // 更新库存
            inventoryRecordService.updateById(one.setEquipmentInventory(one.getEquipmentInventory() + 1));
        } else {
            EquipmentInventoryRecord inventoryRecord = new EquipmentInventoryRecord();
            inventoryRecord.setEquipmentInventory(1)
                    .setEquipmentName(equipment.getEquipmentName())
                    .setCreateTime(LocalDateTime.now())
                    .setUpdateTime(LocalDateTime.now());
            inventoryRecordService.save(inventoryRecord);
        }

        return ResultVO.success("添加成功！");
    }

    @RequestMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<?> delete(Integer equipmentId) {

        Equipment byId = equipmentService.getById(equipmentId);
        if (byId == null) {
            return ResultVO.success("删除失败！无此器材");
        }
        equipmentService.removeById(equipmentId);
        EquipmentInventoryRecord one = inventoryRecordService.getOne(new LambdaQueryWrapper<EquipmentInventoryRecord>().
                eq(EquipmentInventoryRecord::getEquipmentName, byId.getEquipmentName()));
        if (one != null && one.getEquipmentInventory() > 0) {
            // 更新库存
            inventoryRecordService.updateById(one.setEquipmentInventory(one.getEquipmentInventory() - 1));
        }
        return ResultVO.success("删除成功！");
    }

    @RequestMapping("/update")
    public ResultVO<?> update(@RequestBody Equipment equipment) {

        equipmentService.updateById(equipment);
        return ResultVO.success("更新成功！");
    }

}
